package org.jeecg.modules.demo.applyinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.checkerframework.checker.units.qual.A;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.demo.applyinfo.entity.ZnzfDetermineApart;
import org.jeecg.modules.demo.applyinfo.mapper.ZnzfDetermineApartMapper;
import org.jeecg.modules.demo.applyinfo.service.IZnzfDetermineApartService;
import org.jeecg.modules.demo.applyinfo.vo.DetermineApartVo;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 框定单位
 * @Author: jeecg-boot
 * @Date:   2022-03-18
 * @Version: V1.0
 */
@Service
public class ZnzfDetermineApartServiceImpl extends ServiceImpl<ZnzfDetermineApartMapper, ZnzfDetermineApart> implements IZnzfDetermineApartService {

    @Autowired
    private ZnzfDetermineApartMapper znzfDetermineApartMapper;

    @Autowired
    private SysDepartMapper sysDepartMapper;


    @Override
    public ZnzfDetermineApart searchByApplyListIdAndApartId(String apply_list_id, String apart_id) {
        LambdaQueryWrapper<ZnzfDetermineApart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfDetermineApart::getApplyListId, apply_list_id).eq(ZnzfDetermineApart::getApartId, apart_id);
        return znzfDetermineApartMapper.selectOne(queryWrapper);
    }


    @Override
    public List<String> getDepartsByApplyId(String applyId) {
        QueryWrapper<ZnzfDetermineApart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("apply_list_id",applyId);
        List<ZnzfDetermineApart> determineApartList = znzfDetermineApartMapper.selectList(queryWrapper);
        List<String> departList = new ArrayList<>();
        for(int i =0;i < determineApartList.size();i ++){
            String departId = determineApartList.get(i).getApartId();
            departList.add(departId);
        }
        return departList;
    }

    /**
     * 根据框定单位列表id更新所有部门id
     *
     * @param determineApartVo
     * @return
     */
    @Override
    public void updateDepartIds(DetermineApartVo determineApartVo) {
        QueryWrapper<ZnzfDetermineApart> wrapper = new QueryWrapper<>();
        wrapper.eq("apply_list_id",determineApartVo.getApplyId());
        znzfDetermineApartMapper.delete(wrapper);
        for(int i = 0;i < determineApartVo.getDepartIds().length; i++){
            ZnzfDetermineApart determineApart = new ZnzfDetermineApart();
            determineApart.setApplyListId(determineApartVo.getApplyId());
            determineApart.setApartId(determineApartVo.getDepartIds()[i]);
            znzfDetermineApartMapper.insert(determineApart);
        }
    }

    @Override
    public List<ZnzfDetermineApart> searchsByApplyListId(String apply_list_id) {
        LambdaQueryWrapper<ZnzfDetermineApart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnzfDetermineApart::getApplyListId,apply_list_id);
        return znzfDetermineApartMapper.selectList(wrapper);
    }

    @Override
    public List<ZnzfDetermineApart> searchsByApartId(String apart_id) {
        LambdaQueryWrapper<ZnzfDetermineApart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnzfDetermineApart::getApartId,apart_id);
        return znzfDetermineApartMapper.selectList(wrapper);
    }
}
